<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">

    <div class="form-group">
        <label class="control-label col-xs-12 col-sm-2">选择时间:</label>
        <div class="col-xs-12 col-sm-8">
            <input data-rule="required" id="date_range" type="text" class="form-control datetimerange" data-time-picker="false" placeholder="选择时间" name="row[date_range]" date-show-dropdowns="true" value="" autocomplete="off" /> 
        </div>
    </div>
    <div class="form-group" id="channel" data-group='{$qdywys}'>
        <label class="control-label col-xs-12 col-sm-2">分发渠道:</label>
        <div class="col-xs-12 col-sm-8">
            
            <select  id="c-channel" data-rule="required" class="form-control selectpicker" name="row[channel]">
                <option value="">请选择</option>
                {foreach name="channel" item="vo"}
                <option value="{$vo.id}">{$vo.name}</option>
                {/foreach}
            </select>

        </div>
    </div>
    <div class="form-group" id="ywy" style="display: none;">
        <label class="control-label col-xs-12 col-sm-2">渠道业务员:</label>
        <div class="col-xs-12 col-sm-8">
            
            <select  id="c-ywy" data-rule="required" class="form-control selectpicker" name="row[ywy]">
                <option value="">请选择</option>
            </select>

        </div>
    </div>
    <input type="hidden" id="hidden" value="0">
    <div class="form-group layer-footer">
        <label class="control-label col-xs-12 col-sm-2"></label>
        <div class="col-xs-12 col-sm-8">
            <input type="button" id="search" name="button" value="{:__('OK')}" class="btn btn-success btn-embossed">
        </div>
    </div>
</form>
<script src="__CDN__/assets/libs/jquery/dist/jquery.min.js"></script>
<script type="text/javascript">
    // 判断店铺有没有重复
    $('#c-channel').change(function () {
        var val = $('#channel').attr('data-group');
        var obj = $.parseJSON(val);
        console.log(obj)
        var text = $('#c-channel').find('option:selected').text()
        console.log(obj[text])
        if (obj[text].length>1) {
            var channelarr = '';
            for (var i = 0; i < obj[text].length; i++) {
                var name=obj[text][i].nickname
                var id=obj[text][i].id
                channelarr+='<option value="'+id+'">'+name+'</option>'
            }
            var html = `
                    <select  id="c-ywy" data-rule="required" class="form-control selectpicker" name="row[ywy]" onchange="choose(this)">
                        <option value="">请选择</option>`+channelarr+
                    `</select>`;
            $("#ywy .col-sm-8").html(html);
            $('#ywy').show()
        } else {
            $('#hidden').val(0)
            $('#ywy').hide()
        }
    })
    $('#search').click(function () {
        var date_range=$('#date_range').val()
        if (date_range.length==0) {
            alert('请选择时间')
            return false
        }
        var channel = $('#c-channel').find('option:selected').val();
        var ywy = $('#hidden').val();
        if (!channel) {
            alert('请选择渠道')
            return false
        }
        if (!$('#ywy').is(':hidden') && ywy==0) {
            alert('请选择渠道业务员')
            return false
        }
        $.ajax({
            url: "shops/repetitive/add",
            type: 'post',
            dataType: 'json',
            data: {date: date_range,channel:channel,ywy:ywy},
            success: function (ret) {
                if (ret.code==0) {
                    alert(ret.msg)
                } else {
                    console.log(ret.data)
                    if (ret.data.length>0) {
                        var text = ''
                        for (var i = ret.data.length - 1; i >= 0; i--) {
                            text += ret.data[i]+'\r\n'
                        }
                        copyToClipboard_js(text)
                        alert('消息已复制到剪贴板，可直接粘贴')
                    } else {
                        alert('没有可以复制的消息')
                    }
                }
            }, error: function (e) {
                alert('请求出错');
            }
        });
    })
    function copyToClipboard_js(txt) {
        var temp = document.createElement("textarea"); //声明创建一个input元素
        document.body.appendChild(temp); //在body中追加input元素
        temp.value = txt; //把要复制的文字赋予input元素
        temp.select(); //选择要复制的文字
        document.execCommand("copy"); //把文字复制到剪贴板
        document.body.removeChild(temp); //移除body追加的input元素
    }
    function choose(e) {
        $('#hidden').val($(e).val())
        console.log($('#hidden').val())
    }
</script>
